<?php

namespace Validate;


abstract class BaseValidator
{

    public $field;

    public $rules;

    public $method;

    protected $isFail = TRUE;

    protected $message = '';

    protected $params = [];

    /**
     * @return mixed
     */
    protected function getParams()
    {
        return $this->params;
    }

    /**
     * @param $data
     * @return $this
     */
    public function setParams($data)
    {
        $this->params = $data;
        return $this;
    }

    /**
     * @param $message
     * @return bool
     */
    protected function addError($message)
    {
        $this->isFail = FALSE;

        $message = str_replace(':attribute', $this->field, $message);

        $this->message = $message;

        return $this->isFail;
    }

    /**
     * @return string
     */
    public function getError()
    {
        return $this->message;
    }

    /**
     * @param $name
     * @param $value
     * @throws \Exception
     */
    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        if (method_exists($this, $method)) {
            $this->$method($value);
        } else if (property_exists($this, $name)) {
            $this->$name = $value;
        } else {
            throw new \Exception('unknown property ' . $name . ' in class' . get_called_class());
        }
    }
}